package com.example.chapter05;

import androidx.appcompat.app.AppCompatActivity;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

/**
 * TimePickerActivity类用于演示如何使用TimePicker和TimePickerDialog来选择时间
 */
public class TimePickerActivity extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {

    // 定义TimePicker和TextView对象
    private TimePicker tp_time, tp_time_spinner;
    private TextView tv_time, tv_time_spinner, tv_time_dialog;

    /**
     * 初始化组件并设置监听器
     *
     * @param savedInstanceState 保存的实例状态
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_picker);

        // 为按钮设置点击监听器: 直接显示, Spinner显示, 对话框显示
        findViewById(R.id.btn_time).setOnClickListener(this);
        findViewById(R.id.btn_time_spinner).setOnClickListener(this);
        findViewById(R.id.btn_time_dialog).setOnClickListener(this);

        // 初始化TimePicker和TextView组件
        tp_time = findViewById(R.id.tp_time);
        tp_time_spinner = findViewById(R.id.tp_time_spinner);
        // tp_time_spinner.setIs24HourView(true);

        tv_time = findViewById(R.id.tv_time);
        tv_time_spinner = findViewById(R.id.tv_time_spinner);
        tv_time_dialog = findViewById(R.id.tv_time_dialog);
    }

    /**
     * 处理按钮点击事件，根据不同的按钮显示不同的时间选择方式
     *
     * @param v 被点击的视图
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_time) {
            // 1. 直接显示
            String desc = String.format("TimePicker选择的时间是：%d时%d分",
                    tp_time.getCurrentHour(), tp_time.getCurrentMinute());
            tv_time.setText(desc);
        } else if (v.getId() == R.id.btn_time_spinner) {
            String desc = String.format("TimePicker选择的时间是：%d时%d分",
                    tp_time_spinner.getCurrentHour(), tp_time_spinner.getCurrentMinute());
            tv_time_spinner.setText(desc);
        } else {
            // 对话框形式
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            // （1）创建TimePickerDialog对象
//            TimePickerDialog dialog = new TimePickerDialog(this, this, hour, minute, true);
            // （2）换成Holo主题
            TimePickerDialog dialog = new TimePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog, this, hour, minute, true);
            dialog.show();
        }
    }

    /**
     * 处理TimePickerDialog的时间设置事件，更新TextView显示选择的时间
     *
     * @param view      触发事件的TimePicker视图
     * @param hourOfDay 选择的小时
     * @param minute    选择的分钟
     */
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String desc = String.format("TimePickerDialog选择的时间是：%d时%d分", hourOfDay, minute);
        tv_time_dialog.setText(desc);
    }
}
